<?php
/**
 * Singleton class.
 * 
 * LICENSE:
 * 
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with This library. If not, see <{@link http://www.gnu.org/licenses/}>.
 *
 * @link http://code.google.com/p/phpstartapp/
 * @author Bojan Mauser <bmauser@gmail.com>
 * @copyright Bojan Mauser 2009
 * @package psa
 * @version $Id: Psa_Logger.class.php 160 2008-11-30 21:44:05Z bmauser $
 * @ignore 
 */


/**
 * Singleton class.
 * Classes that needs to implement singleton pattern can extend this class.
 * 
 * @see http://en.wikipedia.org/wiki/Singleton_pattern
 * @ignore 
 */
class Psa_Singleton{
	
	/**
	 * Array with instances of objects. This object and child objects.
	 * 
	 * @var array
	 * @ignore
	 */
	static private $instances = array();
		
	
	/**
	 * Constructor.
	 * The constructor is defined as protected that a instance of the this class
	 * cannot be created using the new operator from outside the class.
	 */
	protected function __construct(){}
	

	/**
	 * Returns object's instance.
	 * You should statically call this method with scope resolution operator (::) which gives you
	 * instance to the object from any scope in your application.
	 * Example:
	 * <code>
	 * $instance = Psa_Singleton::get_instance();
	 * </code>
	 * 
	 * @return object
	 */
	static function get_instance($classname = null)
	{	
		// determine object name
		if(!$classname)
			$classname = __CLASS__;
		
		// make new instance of object if not exists
		if (!isset(self::$instances[$classname])){
			self::$instances[$classname] = new $classname();
		}
		// return existing instance
		return self::$instances[$classname];	
	}

	
	/**
	 * Clone method.
	 * Defined as private that a second instance of the {@link Psa_Singleton} class
	 * cannot be created using the clone operator.
	 * @ignore
	 */
	protected function __clone(){}
}

